1
|
|
|
import { useEffect, useState } from "react"; |
2
|
|
|
import { useSelector } from "react-redux"; |
3
|
|
|
import { DispatchType } from "../configureStore"; |
4
|
|
|
import { |
5
|
|
|
Criteria, |
6
|
|
|
Department, |
7
|
|
|
Job, |
8
|
|
|
JobPosterKeyTask, |
9
|
|
|
Skill, |
10
|
|
|
} from "../models/types"; |
11
|
|
|
import { |
12
|
|
|
getDepartments, |
13
|
|
|
departmentsIsLoading, |
14
|
|
|
} from "../store/Department/deptSelector"; |
15
|
|
|
import { getDepartments as fetchDepartments } from "../store/Department/deptActions"; |
16
|
|
|
import { |
17
|
|
|
getCriteriaByJob, |
18
|
|
|
getCriteriaForJobIsUpdating, |
19
|
|
|
getJob, |
20
|
|
|
getJobIsUpdating, |
21
|
|
|
getTasksByJob, |
22
|
|
|
getTasksForJobIsUpdating, |
23
|
|
|
} from "../store/Job/jobSelector"; |
24
|
|
|
import { RootState } from "../store/store"; |
25
|
|
|
import { |
26
|
|
|
fetchCriteria, |
27
|
|
|
fetchJob, |
28
|
|
|
setSelectedJob, |
29
|
|
|
} from "../store/Job/jobActions"; |
30
|
|
|
import { getSkills, getSkillsUpdating } from "../store/Skill/skillSelector"; |
31
|
|
|
import { fetchSkills } from "../store/Skill/skillActions"; |
32
|
|
|
|
33
|
|
|
export function useLoadJob( |
34
|
|
|
jobId: number | null, |
35
|
|
|
dispatch: DispatchType, |
36
|
|
|
): { job: Job | null; isLoadingJob: boolean } { |
37
|
|
|
const job = useSelector((state: RootState) => |
38
|
|
|
jobId !== null ? getJob(state, { jobId }) : null, |
39
|
|
|
); |
40
|
|
|
const isLoadingJob = useSelector((state: RootState) => |
41
|
|
|
jobId !== null ? getJobIsUpdating(state, jobId) : false, |
42
|
|
|
); |
43
|
|
|
useEffect(() => { |
44
|
|
|
if (jobId !== null && job === null && !isLoadingJob) { |
45
|
|
|
dispatch(fetchJob(jobId)); |
46
|
|
|
} |
47
|
|
|
}, [jobId, job, isLoadingJob, dispatch]); |
48
|
|
|
useEffect(() => { |
49
|
|
|
dispatch(setSelectedJob(jobId)); |
50
|
|
|
}, [jobId, dispatch]); |
51
|
|
|
return { job, isLoadingJob }; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
export function useLoadTasks( |
55
|
|
|
jobId: number | null, |
56
|
|
|
dispatch: DispatchType, |
57
|
|
|
): { tasks: JobPosterKeyTask[]; isLoadingTasks: boolean } { |
58
|
|
|
const tasks = useSelector((state: RootState) => |
59
|
|
|
jobId !== null ? getTasksByJob(state, { jobId }) : [], |
60
|
|
|
); |
61
|
|
|
const isLoadingTasks = useSelector((state: RootState) => |
62
|
|
|
jobId !== null ? getTasksForJobIsUpdating(state, jobId) : false, |
63
|
|
|
); |
64
|
|
|
const [hasFetchedTasks, setHasFetchedTasks] = useState(false); |
65
|
|
|
useEffect((): (() => void) => { |
66
|
|
|
let isSubscribed = true; |
67
|
|
|
if (jobId && tasks.length === 0 && !isLoadingTasks && !hasFetchedTasks) { |
68
|
|
|
setHasFetchedTasks(true); |
69
|
|
|
dispatch(fetchJob(jobId)).catch((): void => { |
70
|
|
|
if (isSubscribed) { |
71
|
|
|
setHasFetchedTasks(false); |
72
|
|
|
} |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
return (): void => { |
76
|
|
|
isSubscribed = false; |
77
|
|
|
}; |
78
|
|
|
}, [jobId, tasks.length, isLoadingTasks, hasFetchedTasks, dispatch]); |
79
|
|
|
return { tasks, isLoadingTasks }; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
export function useLoadCriteria( |
83
|
|
|
jobId: number | null, |
84
|
|
|
dispatch: DispatchType, |
85
|
|
|
): { |
86
|
|
|
criteria: Criteria[]; |
87
|
|
|
isLoadingCriteria: boolean; |
88
|
|
|
} { |
89
|
|
|
const criteria = useSelector((state: RootState) => |
90
|
|
|
jobId ? getCriteriaByJob(state, { jobId }) : [], |
91
|
|
|
); |
92
|
|
|
const isLoadingCriteria = useSelector((state: RootState) => |
93
|
|
|
jobId ? getCriteriaForJobIsUpdating(state, jobId) : false, |
94
|
|
|
); |
95
|
|
|
const [hasFetchedCriteria, setHasFetchedCriteria] = useState(false); |
96
|
|
|
useEffect((): (() => void) => { |
97
|
|
|
let isSubscribed = true; |
98
|
|
|
if ( |
99
|
|
|
jobId && |
100
|
|
|
criteria.length === 0 && |
101
|
|
|
!isLoadingCriteria && |
102
|
|
|
!hasFetchedCriteria |
103
|
|
|
) { |
104
|
|
|
setHasFetchedCriteria(true); |
105
|
|
|
dispatch(fetchCriteria(jobId)).catch((): void => { |
106
|
|
|
if (isSubscribed) { |
107
|
|
|
setHasFetchedCriteria(false); |
108
|
|
|
} |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
return (): void => { |
112
|
|
|
isSubscribed = false; |
113
|
|
|
}; |
114
|
|
|
}, [jobId, criteria.length, isLoadingCriteria, hasFetchedCriteria, dispatch]); |
115
|
|
|
return { criteria, isLoadingCriteria }; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
export function useLoadDepartments( |
119
|
|
|
dispatch: DispatchType, |
120
|
|
|
): { |
121
|
|
|
departments: Department[]; |
122
|
|
|
isLoadingDepartments: boolean; |
123
|
|
|
} { |
124
|
|
|
const departments = useSelector(getDepartments); |
125
|
|
|
const isLoading = useSelector(departmentsIsLoading); |
126
|
|
|
useEffect((): void => { |
127
|
|
|
if (departments.length === 0 && !isLoading) { |
128
|
|
|
dispatch(fetchDepartments()); |
129
|
|
|
} |
130
|
|
|
}, [departments.length, isLoading, dispatch]); |
131
|
|
|
return { departments, isLoadingDepartments: isLoading }; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
export function useLoadSkills( |
135
|
|
|
dispatch: DispatchType, |
136
|
|
|
): { |
137
|
|
|
skills: Skill[]; |
138
|
|
|
isLoadingSkills: boolean; |
139
|
|
|
} { |
140
|
|
|
const skills = useSelector(getSkills); |
141
|
|
|
const isLoading = useSelector(getSkillsUpdating); |
142
|
|
|
useEffect((): void => { |
143
|
|
|
if (skills.length === 0 && !isLoading) { |
144
|
|
|
dispatch(fetchSkills()); |
145
|
|
|
} |
146
|
|
|
}, [skills.length, isLoading, dispatch]); |
147
|
|
|
return { skills, isLoadingSkills: isLoading }; |
148
|
|
|
} |
149
|
|
|
|